<template>
  <div>
    <el-dialog :visible.sync="addEditIsDialog_c" width="90rem !important" @close="resetForm('formName')">
      <el-form :model="ruleForm" :rules="rules" ref="formName" label-width="9rem" class="demo-ruleForm form">
        <el-row>
          <el-col :span="12">
            <el-form-item label="起运地基地：" prop="start" label-width="15rem">
              <el-select @change="getWorkUser" v-model="ruleForm.start" placeholder="请选择起运地" clearable
                :popper-append-to-body="false">
                <el-option v-for="item in baseArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="到达地基地：" prop="end" label-width="15rem">
              <el-select @change="getWorkOutUser" v-model="ruleForm.end" placeholder="请选择到达地" clearable
                :popper-append-to-body="false">
                <el-option v-for="item in baseArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="起运地：" prop="startAddress" label-width="15rem">
              <el-select v-model="ruleForm.startAddress" placeholder="请选择起运地" clearable :popper-append-to-body="false">
                <el-option v-for="item in addresssgetArray" :key="item.id" :label="item.address"
                  :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="到达地：" prop="addressArrival" label-width="15rem">
              <el-select v-model="ruleForm.addressArrival" placeholder="请选择到达地" clearable :popper-append-to-body="false">
                <el-option v-for="item in addresssOutArray" :key="item.id" :label="item.address"
                  :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="装鱼单兵：" prop="workUserId" label-width="15rem">
              <el-select v-model="ruleForm.workUserId" placeholder="请选择单兵" clearable :popper-append-to-body="false">
                <el-option v-for="item in soldiersArray" :key="item.id" :label="item.soldierName"
                  :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="卸鱼单兵：" prop="workOutId" label-width="15rem">
              <el-select v-model="ruleForm.workOutId" placeholder="请选择单兵" clearable :popper-append-to-body="false">
                <el-option v-for="item in soldiersOutArray" :key="item.id" :label="item.soldierName"
                  :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="车辆：" prop="carId" label-width="15rem">
              <el-select v-model="ruleForm.carId" placeholder="请选择车辆" clearable :popper-append-to-body="false"
                @change="carChange">
                <el-option v-for="item in carsArray" :key="item.id" :label="item.carNumber" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="司机：" prop="carDriverId" label-width="15rem">
              <el-select v-model="ruleForm.carDriverId" placeholder="请选择司机" clearable :popper-append-to-body="false">
                <el-option v-for="item in carDriversArray" :key="item.id" :label="item.carUser"
                  :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="装鱼日期：" label-width="15rem">
              <el-date-picker v-model="ruleForm.workInDate" type="date" :editable="false" placeholder="请选择装鱼日期"
                align="right" :picker-options="pickerOptions" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
                style="width: 100%"> </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="卸鱼日期：" label-width="15rem">
              <el-date-picker v-model="ruleForm.workOutDate" type="date" :editable="false" placeholder="请选择卸鱼日期"
                align="right" :picker-options="pickerOptions" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
                style="width: 100%"> </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>

          <el-col :span="12">
            <el-form-item label="装鱼单兵时间：" label-width="15rem">
              <el-time-picker popper-class="time" :append-to-body="false" v-model="ruleForm.workInTime" format="HH:mm:ss"
                value-format="HH:mm:ss" placeholder="请选择装鱼单兵时间">
              </el-time-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="卸鱼单兵时间：" label-width="15rem">
              <el-time-picker popper-class="time" :append-to-body="false" v-model="ruleForm.workOutTime" format="HH:mm:ss"
                value-format="HH:mm:ss" placeholder="请选择卸鱼单兵时间">
              </el-time-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')" class="btn">取 消</el-button>
        <el-button type="primary" @click="submitForm('formName')" class="btn-light-blue btn">确 认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getObjSoldiers, getObjCars, getSysCompanyBaseList, getObjCarDrivers, getObjAddresss } from "@/api/breeding";
export default {
  props: {
    baseId: {
      type: String,
      default: "",
    },
    configurationIsDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      ruleForm: {
        // 表单字段
        workUserId: "",
        workInDate: "",
        workOutDate: "",
        carId: "",
        startAddress: "",
        carDriverId: "",
        workInTime: '',
        workOutTime: '',
        addressArrival: '',
        workOutId: ''
      },
      rules: {
        // 表单规则
      },
      addEditIsDialog_c: false, // 弹窗是否显示
      pickerOptions: {
        shortcuts: [
          {
            text: "今天",
            onClick(picker) {
              picker.$emit("pick", new Date());
            },
          },
          {
            text: "昨天",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit("pick", date);
            },
          },
          {
            text: "一周前",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", date);
            },
          },
        ],
      },
      soldiersArray: [], // 单兵集合
      addresssArray: [], // 地址集合
      carsArray: [], // 车辆集合
      carDriversArray: [], // 司机集合
      soldiersOutArray: [],
      addresssgetArray: [],
      baseArray: [],
      addresssOutArray: []
    };
  },
  mounted() {
    // setTimeout(() => {

    //   this.getObjAddresss()
    // }, 2000);
    this.getSysCompanyBaseList()
  },
  watch: {
    configurationIsDialog(val) {
      this.addEditIsDialog_c = val;
      if (this.addEditIsDialog_c) {
        if (window.localStorage.getItem("allotData")) {
          this.ruleForm = JSON.parse(window.localStorage.getItem("allotData"));
        }
        // this.getObjSoldiers();
        this.getObjCars();
        this.getObjAddresss();
      }
    },
  },
  methods: {
    getSysCompanyBaseList() {
      getSysCompanyBaseList().then((res) => {
        if (res.code == 200) {
          this.baseArray = res.data.data;
        }
      });
    },


    getObjAddresss() {

    },
    // 获取选择状态下的单兵
    getWorkUser(val) {

      this.ruleForm.startAddress = ''
      this.ruleForm.workUserId = ''
      if (val) {
        getObjAddresss({ id: val }).then((res) => {
          if (res.code == 200) {
            this.addresssgetArray = res.data;
          }
        });
        getObjSoldiers({ id: val }).then((res) => {
          if (res.code == 200) {
            this.soldiersArray = res.data;
          }
        });
      }
    },
    getWorkOutUser(val) {

      this.ruleForm.addressArrival = ''
      this.ruleForm.workOutId = ''
      if (val) {
        getObjAddresss({ id: val }).then((res) => {
          if (res.code == 200) {
            this.addresssOutArray = res.data;
          }
        });
        getObjSoldiers({ id: val }).then((res) => {
          if (res.code == 200) {
            this.soldiersOutArray = res.data;
          }
        });
      }
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          window.localStorage.setItem("allotData", JSON.stringify(this.ruleForm));
          this.$parent.closeConfiguration();
        }
      });
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.$parent.closeConfiguration();
    },
    // 获取单兵列表
    // getObjSoldiers() {
    //   getObjSoldiers({ id: this.baseId }).then((res) => {
    //     if (res.code == 200) {
    //       this.soldiersArray = res.data;
    //     }
    //   });
    // },
    // 获取车辆列表
    getObjCars() {
      getObjCars().then((res) => {
        if (res.code == 200) {
          this.carsArray = res.data;
        }
      });

    },
    // 获取地址列表
    // getObjAddresss() {
    //   getSysCompanyBaseList().then((res) => {
    //     if (res.code == 200) {
    //       this.addresssArray = res.data.data;
    //       console.log(this.addresssArray);
    //     }
    //   });
    //   if (this.ruleForm.carId !== "" || this.ruleForm.carId !== null) {
    //     this.getObjCarDrivers(this.ruleForm.carId);
    //   }
    // },
    // 类型切换
    carChange(val) {
      this.ruleForm.carDriverId = "";
      this.carDriversArray = [];

      let obj = this.carsArray.find((item) => {
        return item.id == val + "";
      });
      this.getObjCarDrivers(obj.id);
    },
    // 获取车辆司机列表
    getObjCarDrivers(id) {
      if (this.ruleForm.carId === "" || this.ruleForm.carId == null) {
        return;
      }

      getObjCarDrivers({ id: id }).then((res) => {
        if (res.code == 200) {
          this.carDriversArray = res.data;
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
::v-deep .time {
  /deep/ .el-time-panel {
    width: 25rem !important;
  }

  .el-time-spinner__item {
    font-size: 2rem;
    height: 4rem;
    line-height: 4rem;
  }

  .el-time-panel__content::after,
  .el-time-panel__content::before {
    height: 4rem;
  }

  width: 25rem !important;
}



/deep/ .el-dialog__footer {
  text-align: center;
}

/deep/ .el-dialog__close {
  display: none !important;
}

/deep/.el-dialog__body {
  font-size: 1.86rem !important;
}

.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;
}

.el-cascader {
  width: 100%;
}

.el-select {
  width: 100%;
}

.form {

  // height: 23rem;
  /deep/.el-form-item {
    margin-bottom: 4.79rem !important;
  }

  /deep/.el-input {
    font-size: 2.16rem;
  }

  /deep/.el-input__inner {
    width: 25.64rem !important;
    height: 6rem !important;
    padding-left: 2.542857rem;
  }

  /deep/.el-input__icon {
    line-height: 5.857143rem;
  }

  /deep/.el-input__suffix {
    right: -4.642857rem;
  }

  /deep/ .el-form-item__label {
    font-size: 1.96rem !important;
    line-height: 5rem !important;
  }

  ::v-deep .el-select-dropdown__empty {
    padding: 0.814286rem 3.3rem;
    font-size: 2.16rem;
  }

  ::v-deep .el-select-dropdown__list {
    .el-select-dropdown__item {
      font-size: 2.16rem;
      height: 4.428571rem;
      line-height: 4.428571rem;
    }
  }
}

.input-box {
  display: flex;
  justify-content: space-around;
}

.btn {
  width: 8.65rem;
  height: 4.48rem;
  font-size: 2.19rem;
}
</style>
